package com.zzx.action;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzx.pojo.User;
import com.zzx.service.IUserService;

@Controller
public class UserController {

	@Autowired
	private IUserService userService;
	
	@RequestMapping("/test")
	public String index(User user) {
		System.out.println("*********后台执行****开始***********");
		String name = user.getName();
		
		int result = name.hashCode();
//		
		int hn = Math.abs(result);
		String str2 = String.valueOf(hn).substring(0,2);
		user.setId(UUID.randomUUID().toString().replace("-", ""));
//		//userService.saveUser(user);
//		//int i=(int)(100*Math.random());
		String tableName = "t_user_"+str2;
		user.setTableName(tableName);
//		//userService.createTable(tableName);
		
		if(userService.ifExistsTable(tableName)) {
			System.out.println("****表已经存在****");
			userService.saveUser(user);
		}else {
			System.out.println("****表不存在****");
			userService.createTable(tableName);
		}
//		
		System.out.println("*********后台执行*****结束**********");
		return "index";
	}
	
	@RequestMapping("/setRedis")
	@ResponseBody
	public String setRedisValue() {
		System.out.println("*********redis初始化*********");
		userService.setRedisValue("name", "nana");
		return "index";
	}
	
	@RequestMapping("/getRedis")
	@ResponseBody
	public String getRedisValue(String name) {
		System.out.println("*********redis 获取*********");
		userService.getRedisValue(name);
		return "index";
	}
}
